package com.wdf.dota;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.wdf.bean.Dota2EquipmentDialog;
import com.wdf.bean.Dota2Info;
import com.wdf.bean.Dota2OutofEquInfo;
import com.wdf.bean.DotaEquipment;
import com.wdf.bean.Equipment;

public class Dota2HeroEquiActivity extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dota2_hero_equi);

		try {
		Bundle bundle = this.getIntent().getExtras();
		String heroname =	bundle.getString("heroname");
 
		Dota2Info dota2Info =new Dota2Info();
            Equipment equi = new Equipment();
          //反射获取装备图片名
            String[] start = dota2Info.getClass().getField(heroname+"_1").get(dota2Info).toString().split(",");
            String[] medium = dota2Info.getClass().getField(heroname+"_2").get(dota2Info).toString().split(",");
            String[] end = dota2Info.getClass().getField(heroname+"_3").get(dota2Info).toString().split(",");
            
            //反射获取出装路线说明
            Dota2OutofEquInfo equInfo = new Dota2OutofEquInfo();
            String equ1Text = equInfo.getClass().getField(heroname+"_equ1").get(equInfo).toString();
            String equ2Text = equInfo.getClass().getField(heroname+"_equ2").get(equInfo).toString();
            String equ3Text = equInfo.getClass().getField(heroname+"_equ3").get(equInfo).toString();
            
            TextView startEquText = (TextView) findViewById(R.id.startequText);
            TextView mediumEquText = (TextView) findViewById(R.id.mediumequText);
            TextView endEquText = (TextView) findViewById(R.id.endequText);
            
            startEquText.setText(Html.fromHtml(equ1Text));
            mediumEquText.setText(Html.fromHtml(equ2Text));
            endEquText.setText(Html.fromHtml(equ3Text));
            
          //通过三个for循环部署图片
            for(int i= 0;i< start.length; i++){
            	final String equName= start[i];
            	ImageView  startequipmentmap = (ImageView ) findViewById(getResources().getIdentifier("startequipment"+(i+1), "id", "com.wdf.dota"));
            	InputStream fis;
    			try {
    				fis = getAssets().open( "dota2/"+start[i] +".jpg");
    				Bitmap bm = BitmapFactory.decodeStream(fis);
    				startequipmentmap.setImageBitmap(bm);
					startequipmentmap.setOnClickListener(new OnClickListener(){
							@Override
							public void onClick(View v) {
								      showDisplay(equName);
							}
	            			
	            		}); 
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
            }
            for(int i= 0;i< medium.length; i++){
            	final String equName= medium[i];
            	ImageView  mediumequipmentmap = (ImageView ) findViewById(getResources().getIdentifier("mediumequipment"+(i+1), "id", "com.wdf.dota"));
            	InputStream fis;
    			try {
    				fis = getAssets().open( "dota2/"+medium[i] +".jpg");
    				Bitmap bm = BitmapFactory.decodeStream(fis);
    				mediumequipmentmap.setImageBitmap(bm);
    				mediumequipmentmap.setOnClickListener(new OnClickListener(){
						@Override
						public void onClick(View v) {
							      showDisplay(equName);
						}
            			
            		}); 
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
            }
            
            for(int i= 0;i< end.length; i++){
            	final String equName= end[i];
            	ImageView  endequipmentmap = (ImageView ) findViewById(getResources().getIdentifier("endequipment"+(i+1), "id", "com.wdf.dota"));
            	InputStream fis;
    			try {
    				fis = getAssets().open( "dota2/"+end[i] +".jpg");
    				Bitmap bm = BitmapFactory.decodeStream(fis);
    				endequipmentmap.setImageBitmap(bm);
    				endequipmentmap.setOnClickListener(new OnClickListener(){
						@Override
						public void onClick(View v) {
							      showDisplay(equName);
						}
            			
            		}); 
    			} catch (IOException e) {
    				e.printStackTrace();
    			}

            }
            

            
		} catch (Exception e) {
		}
		
		
	}
	
	public void showDisplay(String item){
		
		final Dota2EquipmentDialog dialog = new Dota2EquipmentDialog(Dota2HeroEquiActivity.this, R.style.MyDialog);
		//	dialog.setProperty(400, 600);
		     dialog.setContentView(R.layout.dota2_equi_dialog);
		     DotaEquipment equipment =new DotaEquipment();
				dialog. setPositiveButton("    ", new android.view.View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
					}
				    });
	   		    InputStream fis;
				try {
					fis = getAssets().open( "dota2/"+item +".jpg");
					Bitmap bm = BitmapFactory.decodeStream(fis);
					dialog.setItemImage(bm);
					String name = equipment.getClass().getField(item+"_name").get(equipment).toString();
					String price = equipment.getClass().getField(item+"_price").get(equipment).toString();
					String content = equipment.getClass().getField(item+"_property").get(equipment).toString();
					dialog.setName(Html.fromHtml(name));
					dialog.setPrice(Html.fromHtml(price));
					dialog.setContent(Html.fromHtml(content));
					
					String[] childnames = (String[])equipment.getClass().getField(item+"_need").get(equipment);
					if(childnames!=null && childnames.length>0){
						 if(childnames.length>=1){
							 String childname = childnames[0];
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname +".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage1(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage1name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage1price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=2){
							 String childname = childnames[1].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname +".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage2(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage2name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage2price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=3){
								String childname = childnames[2].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname+".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage3(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage3name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage3price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=4){
								String childname = childnames[3].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname+".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage3(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage3name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage3price(Html.fromHtml(childprice));
						 }
						 Object itemReel =equipment.getClass().getField(item+"_reel").get(equipment);
						 if(itemReel !=null){
							 dialog.setReel(Html.fromHtml("卷轴： <font color='#DB8931'>"+itemReel+"</font>"));
						 }
						
						
					}else{
					
						
					}
					
	
				} catch (Exception e) {
					e.printStackTrace();
				}
				
		     dialog.show();
	}

}
